import React, { useState } from 'react';
import NumberField from '../../../../components/common/NumberField';
import StatefulCalculationCard from '../../../../components/common/StatefulCalculationCard';
import ExportData from '../../../../components/common/ExportCalculateData';
import type { ResultItem } from '../../../../../shared/types';

// 定义顶部岩层计算表单数据类型
export interface TopFormValues {
  gamma_m: number;
  l: number;
  sigma_t: number;
  tau: number;
  q: number;
  k_s: number;
}

// 定义底部岩层计算表单数据类型
export interface BottomFormValues extends TopFormValues {}

// 定义侧壁岩层计算表单数据类型
export interface SideFormValues {
  k_s: number;
  l: number;
  sigma_t: number;
  tau: number;
  k_af: number;
  gamma_f: number;
  h: number;
  p: number;
  gamma_w: number;
}

// 顶部岩层计算初始值配置
const DEFAULT_TOP: TopFormValues = { gamma_m: 25.0, l: 5.0, sigma_t: 1000.0, tau: 500.0, q: 500.0, k_s: 1.3 };

// 底部岩层计算初始值配置
const DEFAULT_BOTTOM: BottomFormValues = { ...DEFAULT_TOP };

// 侧壁岩层计算初始值配置
const DEFAULT_SIDE: SideFormValues = { k_s: 1.2, l: 5.0, sigma_t: 1000.0, tau: 500.0, k_af: 1.0, gamma_f: 18.0, h: 10.0, p: 50.0, gamma_w: 10.0 };

// 顶部岩层计算公式
const TOP_FORMULA = 'T_s=\\frac{\\gamma_m \\cdot l^2 + q}{k_s \\cdot(\\sigma_t - \\tau)}';

// 底部岩层计算公式
const BOTTOM_FORMULA = 'T_s=\\frac{\\gamma_m \\cdot l^2 + q}{k_s \\cdot(\\sigma_t - \\tau)}';

// 侧壁岩层计算公式
const SIDE_FORMULA = 'T_s=\\frac{k_{af} \\cdot(\\gamma_f \\cdot h + p - \\gamma_w) \\cdot l}{k_s \\cdot(\\sigma_t - \\tau)}';

// 顶部岩层计算卡片组件配置
const TopRockStratumConfig = {
  key: 'TopRockStratumCalculation',
  label: '顶部岩层安全厚度计算',
  formKey: 'thickness-rock-stratum-top',
  initialValues: DEFAULT_TOP,
  calculationType: 'thickness-rock-stratum-top',
  resultFormatter: (data: any) => [
    { key: 'T_s', name: '安全厚度Tₛ', value: data.T_s, unit: 'm' }
  ],
  renderFormFields: () => (
    <>
      <NumberField name="gamma_m" label="上覆岩体容重γₘ (kN/m³)" required min={0} step={0.1} tooltip="上覆岩体的容重" />
      <NumberField name="l" label="洞径l (m)" required min={0} step={0.1} tooltip="隧道的直径" />
      <NumberField name="sigma_t" label="岩体抗拉强度σₜ (kPa)" required min={0} step={0.1} tooltip="岩体的抗拉强度" />
      <NumberField name="tau" label="岩体粘聚力τ (kPa)" required min={0} step={0.1} tooltip="岩体的粘聚力" />
      <NumberField name="q" label="均布荷载q (kPa)" required min={0} step={0.1} tooltip="作用在岩层上的均布荷载" />
      <NumberField name="k_s" label="安全系数kₛ" required min={0} step={0.1} tooltip="计算安全系数" />
    </>
  ),
  formula: TOP_FORMULA
};

// 底部岩层计算卡片组件配置
const BottomRockStratumConfig = {
  key: 'BottomRockStratumCalculation',
  label: '底部岩层安全厚度计算',
  formKey: 'thickness-rock-stratum-bottom',
  initialValues: DEFAULT_BOTTOM,
  calculationType: 'thickness-rock-stratum-bottom',
  resultFormatter: (data: any) => [
    { key: 'T_s', name: '安全厚度Tₛ', value: data.T_s, unit: 'm' }
  ],
  renderFormFields: () => (
    <>
      <NumberField name="gamma_m" label="上覆岩体容重γₘ (kN/m³)" required min={0} step={0.1} tooltip="上覆岩体的容重" />
      <NumberField name="l" label="洞径l (m)" required min={0} step={0.1} tooltip="隧道的直径" />
      <NumberField name="sigma_t" label="岩体抗拉强度σₜ (kPa)" required min={0} step={0.1} tooltip="岩体的抗拉强度" />
      <NumberField name="tau" label="岩体粘聚力τ (kPa)" required min={0} step={0.1} tooltip="岩体的粘聚力" />
      <NumberField name="q" label="均布荷载q (kPa)" required min={0} step={0.1} tooltip="作用在岩层上的均布荷载" />
      <NumberField name="k_s" label="安全系数kₛ" required min={0} step={0.1} tooltip="计算安全系数" />
    </>
  ),
  formula: BOTTOM_FORMULA
};

// 侧壁岩层计算卡片组件配置
const SideRockStratumConfig = {
  key: 'SideRockStratumCalculation',
  label: '侧壁岩层安全厚度计算',
  formKey: 'thickness-rock-stratum-side',
  initialValues: DEFAULT_SIDE,
  calculationType: 'thickness-rock-stratum-side',
  resultFormatter: (data: any) => [
    { key: 'T_s', name: '安全厚度Tₛ', value: data.T_s, unit: 'm' }
  ],
  renderFormFields: () => (
    <>
      <NumberField name="k_s" label="安全系数kₛ" required min={0} step={0.1} tooltip="计算安全系数" />
      <NumberField name="l" label="洞径l (m)" required min={0} step={0.1} tooltip="隧道的直径" />
      <NumberField name="sigma_t" label="岩体抗拉强度σₜ (kPa)" required min={0} step={0.1} tooltip="岩体的抗拉强度" />
      <NumberField name="tau" label="岩体粘聚力τ (kPa)" required min={0} step={0.1} tooltip="岩体的粘聚力" />
      <NumberField name="k_af" label="侧压系数kₐf" required min={0} step={0.01} tooltip="侧向压力系数" />
      <NumberField name="gamma_f" label="回填土容重γf (kN/m³)" required min={0} step={0.1} tooltip="回填土的容重" />
      <NumberField name="h" label="回填土高度h (m)" required min={0} step={0.1} tooltip="回填土的高度" />
      <NumberField name="p" label="附加荷载p (kPa)" required min={0} step={0.1} tooltip="附加在回填土上的荷载" />
      <NumberField name="gamma_w" label="水的容重γw (kN/m³)" required min={0} step={0.1} tooltip="水的容重" />
    </>
  ),
  formula: SIDE_FORMULA
};

// 顶部岩层计算卡片组件实现
export const ThicknessRockStratumTopCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={TopRockStratumConfig.label}
      formKey={TopRockStratumConfig.formKey}
      initialValues={TopRockStratumConfig.initialValues}
      renderFormFields={TopRockStratumConfig.renderFormFields}
      calculationType={TopRockStratumConfig.calculationType}
      resultFormatter={(data) => {
        const results = TopRockStratumConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={1}
      tableHeight={300}
      formula={TopRockStratumConfig.formula}
      exportComponent={(form) => (
        <ExportData
          siteName={TopRockStratumConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={TopRockStratumConfig.calculationType}
        />
      )}
    />
  );
};

// 底部岩层计算卡片组件实现
export const ThicknessRockStratumBottomCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={BottomRockStratumConfig.label}
      formKey={BottomRockStratumConfig.formKey}
      initialValues={BottomRockStratumConfig.initialValues}
      renderFormFields={BottomRockStratumConfig.renderFormFields}
      calculationType={BottomRockStratumConfig.calculationType}
      resultFormatter={(data) => {
        const results = BottomRockStratumConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={1}
      tableHeight={300}
      formula={BottomRockStratumConfig.formula}
      exportComponent={(form) => (
        <ExportData
          siteName={BottomRockStratumConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={BottomRockStratumConfig.calculationType}
        />
      )}
    />
  );
};

// 侧壁岩层计算卡片组件实现
export const ThicknessRockStratumSideCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={SideRockStratumConfig.label}
      formKey={SideRockStratumConfig.formKey}
      initialValues={SideRockStratumConfig.initialValues}
      renderFormFields={SideRockStratumConfig.renderFormFields}
      calculationType={SideRockStratumConfig.calculationType}
      resultFormatter={(data) => {
        const results = SideRockStratumConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={1}
      tableHeight={300}
      formula={SideRockStratumConfig.formula}
      exportComponent={(form) => (
        <ExportData
          siteName={SideRockStratumConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={SideRockStratumConfig.calculationType}
        />
      )}
    />
  );
};